angular.module('nonghua.controllers.village', [])

    .controller('VillageActivityListCtrl', function ($scope, $ionicSideMenuDelegate, $stateParams,$rootScope) {
        $scope.access={
            list:$rootScope.user_access('view own village_activity'),
            add:$rootScope.user_access('add village_activity'),
        }
        $scope.addressOption = {
            province: '',
            city: '',
            county: '',
            town: '',
            village: '',
            level:5
        };
        $scope.dateOption={
            year:'',
            month:'',
            day:'',
            level:2
        };
        $scope.updateFilter = function() {
            $scope.filterParams =
            {
                province: $scope.addressOption.province,
                city: $scope.addressOption.city,
                county: $scope.addressOption.county,
                town: $scope.addressOption.town,
                village: $scope.addressOption.village,
                year: $scope.dateOption.year,
                month: $scope.dateOption.month,
            };
        };
        $scope.moduleTitle = "村落活动";
        $scope.moduleName = "village_activity";
        listCtrl($scope, $rootScope,"village_activity_id","villageActivityList", "village_activity_list", $ionicSideMenuDelegate);
    })
    .controller('VillageActivityDetailCtrl', function ($scope,$stateParams,$rootScope,$ionicLoading,$state,$ionicHistory) {
        var listStateName = "village-activity-list";
        var resourceName = "village_activity_add";
        $scope.listName = "villageActivityList";
        $scope.moduleTitle = "村落活动";
        $scope.moduleName = "village_activity";

        $scope.id = $stateParams.id != "0" ? $stateParams.id : '';
        if(!empty($scope.id)){
            $scope.operation = "修改";
            $scope.detail = $rootScope[$scope.listName][$scope.id];
        }else{
            $scope.operation = "添加";
            $scope.detail={};
        }
        $scope.access={
            list:$rootScope.user_access('view own village_activity'),
            add:$rootScope.user_access('add village_activity'),
        }
        $scope.addressOption = {
            province: '',
            city: '',
            county: '',
            town: '',
            village: '',
            init_addr_id:!empty($scope.detail) && !empty($scope.detail.addr_id) ? $scope.detail.addr_id : "",
            level:5
        };
        $scope.distributorOption={
            value:'',
            params:
            {
                action: "get_distributor_all" //'plant','fertilizer','product','brand','sales_type','department_employee'
            },
            label:'经销商',
            initValue:!empty($scope.detail) && !empty($scope.detail.distributor_id) ? $scope.detail.distributor_id : ''
        };



        $scope.dateOption={
            year:'',
            initYear:!empty($scope.detail) && !empty($scope.detail.date) ? get_year_month_day_from_date($scope.detail.date)['year'] : '',
            initMonth:!empty($scope.detail) && !empty($scope.detail.date) ? get_year_month_day_from_date($scope.detail.date)['month'] : '',
            initDay:!empty($scope.detail) && !empty($scope.detail.date) ? get_year_month_day_from_date($scope.detail.date)['day'] : '',
            month:'',
            day:'',
            level:3
        };

        $scope.submitForm = function (form) {
            var path_to_source = 'mobile_service_resources/'+resourceName;
            var params = {
                id: !empty($scope.id) ? $scope.id : "",
                addr_id: _get_address_id($scope.addressOption),
                distributor_id: $scope.distributorOption.value,
                date: $scope.dateOption.year+"-"+$scope.dateOption.month+"-"+$scope.dateOption.day,
                title: $scope.detail.title,
                body: $scope.detail.body,
                attendee: $scope.detail.attendee,
                comments: $scope.detail.comments,
            };
            console.log(params);
            $ionicLoading.show({
                content: '请稍后',
                animation: 'fade-in',
                showBackdrop: true,
                maxWidth: 200,
                showDelay: 0
            });
            _get_datasouce_result({
                path: path_to_source,
                param: params,
                resource: resourceName,
                success: function (data) {
                    $ionicLoading.hide();
                    if (data.option) {
                        $ionicHistory.clearCache().then(function(){ $state.go(listStateName);});
                        alert("操作成功！");
                    } else {
                        var msg = ""
                        if(data.errorNum == 3){
                            msg = "无权限！"
                        }
                        alert("操作失败！"+msg);
                    }
                },
                error: function (xhr, status, message) {
                    $ionicLoading.hide();
                    $state.go('error');
                }
            });
        }
    })


    .controller('ExpertsWorkListCtrl', function ($scope, $ionicSideMenuDelegate, $stateParams,$rootScope) {
        $scope.access={
            list:$rootScope.user_access('view own experts_work'),
            add:$rootScope.user_access('add experts_work'),
        }
        $scope.dateOption={
            year:'',
            month:'',
            day:'',
            level:2
        };
        $scope.updateFilter = function() {
            $scope.filterParams =
            {
                year: $scope.dateOption.year,
                month: $scope.dateOption.month,
            };
        };
        $scope.moduleTitle = "专家记录";
        $scope.moduleName = "experts_work";
        listCtrl($scope, $rootScope,"experts_work_id","expertsWorkList", "experts_work_list", $ionicSideMenuDelegate);
    })
    .controller('ExpertsWorkDetailCtrl', function ($scope,$stateParams,$rootScope,$ionicLoading,$state,$ionicHistory) {
        var listStateName = "experts-work-list";
        var resourceName = "experts_work_add";
        $scope.listName = "expertsWorkList";
        $scope.moduleTitle = "专家记录";
        $scope.moduleName = "experts_work";

        $scope.id = $stateParams.id != "0" ? $stateParams.id : '';
        if(!empty($scope.id)){
            $scope.operation = "修改";
            $scope.detail = $rootScope[$scope.listName][$scope.id];
        }else{
            $scope.operation = "添加";
            $scope.detail={};
        }
        $scope.access={
            list:$rootScope.user_access('view own experts_work'),
            add:$rootScope.user_access('add experts_work'),
        }
        $scope.dateOption={
            year:'',
            initYear:!empty($scope.detail) && !empty($scope.detail.work_date) ? get_year_month_day_from_date($scope.detail.work_date)['year'] : '',
            initMonth:!empty($scope.detail) && !empty($scope.detail.work_date) ? get_year_month_day_from_date($scope.detail.work_date)['month'] : '',
            initDay:!empty($scope.detail) && !empty($scope.detail.work_date) ? get_year_month_day_from_date($scope.detail.work_date)['day'] : '',
            month:'',
            day:'',
            level:3
        };
        $scope.expertsOption={
            value:'',
            params:
            {
                action: "get_experts" //'plant','fertilizer','product','brand','sales_type','department_employee'
            },
            label:'参与专家',
            initValue:!empty($scope.detail) && !empty($scope.detail.experts_id) ? $scope.detail.experts_id : ''
        };
        $scope.submitForm = function (form) {
            var path_to_source = 'mobile_service_resources/'+resourceName;
            var params = {
                experts_work_id: !empty($scope.id) ? $scope.id : "",
                address:$scope.detail.address,
                work_date: $scope.dateOption.year+"-"+$scope.dateOption.month+"-"+$scope.dateOption.day,
                title: $scope.detail.title,
                experts_id:$scope.expertsOption.value,
                content: $scope.detail.content,
                attendees: $scope.detail.attendees,
            };
            console.log(params);
            $ionicLoading.show({
                content: '请稍后',
                animation: 'fade-in',
                showBackdrop: true,
                maxWidth: 200,
                showDelay: 0
            });
            _get_datasouce_result({
                path: path_to_source,
                param: params,
                resource: resourceName,
                success: function (data) {
                    $ionicLoading.hide();
                    if (data.option) {
                        $ionicHistory.clearCache().then(function(){ $state.go(listStateName);});
                        alert("操作成功！");
                    } else {
                        var msg = ""
                        if(data.errorNum == 3){
                            msg = "无权限！"
                        }
                        alert("操作失败！"+msg);
                    }
                },
                error: function (xhr, status, message) {
                    $ionicLoading.hide();
                    $state.go('error');
                }
            });
        }
    })